package com.coinland.study.view;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.coinland.study.R;
import com.coinland.study.base.BaseActivity;
import com.coinland.study.util.PermissionUtil;
import com.coinland.study.util.ToastUtil;
import com.coinland.study.widgt.ListViewHeadDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.BezierCircleHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements OnRefreshListener {

    private String TAG = getClass().getSimpleName();

    private List<String> mData;

    @BindView(R.id.recycleView)
    public RecyclerView recyclerView;


    @BindView(R.id.refresh_layout)
    public SmartRefreshLayout smartRefreshLayout;

    @BindView(R.id.refreshCircle_header)
    public BezierCircleHeader refreshCircleHeader;

    @BindView(R.id.iv_back)
    public ImageView ivBack;

    @BindView(R.id.tv_sub)
    public TextView tvSub;

    @BindView(R.id.tv_title)
    public TextView tvTitle;

    @OnClick({R.id.iv_back, R.id.tv_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_sub:
//                startActivity(new Intent(this, WebViewActivity.class));
                startActivity(new Intent(this, DownloadFileActivity.class));
                break;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 检查权限
        checkPermission();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        ivBack.setVisibility(View.INVISIBLE);
        tvTitle.setText("《*主页*》");
        tvSub.setVisibility(View.VISIBLE);
        tvSub.setText("WORD文档下载");

        initContent();
        initSmartRefresh();
    }

    /**
     * 下拉刷新
     */
    private void initSmartRefresh() {
        smartRefreshLayout.setEnableHeaderTranslationContent(true);
        smartRefreshLayout.setEnableFooterTranslationContent(false);
        smartRefreshLayout.setHeaderHeight(120);
        smartRefreshLayout.setDragRate(0.5f);
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setPrimaryColorsId(R.color.transparent, android.R.color.white);
    }


    /**
     * 下拉刷新
     *
     * @param refreshLayout
     */
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        // 模拟获取网络数据，延时1s
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mData.clear();
                        char letter = 'A';
                        for (int i = 0; i < 26; i++) {
                            mData.add(String.valueOf(letter));
                            letter++;
                        }
                        refreshLayout.finishRefresh();
                        recyclerView.getAdapter().notifyDataSetChanged();
                    }
                });
            }
        }, 2000);

    }


    public void initContent() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置方向,默认方向(垂直)
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        mData = new ArrayList<>();
        char letter = 'A';
        for (int i = 0; i < 26; i++) {
            mData.add(String.valueOf(letter) + "++");
            letter++;
        }

        //自定义分割线,顶部有线
        recyclerView.addItemDecoration(new ListViewHeadDecoration(this, R.drawable.listview_decoration));
    }


    /**
     * 检查权限
     */
    private void checkPermission() {
        PermissionUtil.requestPermission(this, new PermissionUtil.IPermissionListener() {
            @Override
            public void permissionGranted() {
                getSubscriberId();
            }

            @Override
            public void permissionDenied() {
                ToastUtil.showShort("权限被拒绝", MainActivity.this);
            }
        }, "权限被拒绝,请设置应用权限", Manifest.permission.READ_PHONE_STATE);
    }


    // 遍历 TelephonyManager 里的方法
    public void printTelephonyManagerMethodNamesForThisDevice() {
        TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Class<?> telephonyClass;
        try {
            telephonyClass = Class.forName(telephony.getClass().getName());
            Method[] methods = telephonyClass.getMethods();
            for (int i = 0; i < methods.length; i++) {
                Log.i(TAG, "\n" + methods[i] + " declared by " + methods[i].getDeclaringClass());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 获取双卡双待 SIM 卡序列号
    public void getSubscriberId() {
        TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Class<?> telephonyClass;
        Object result1 = null;
        Object result2 = null;
        try {
            telephonyClass = Class.forName(telephony.getClass().getName());
            Method m1 = telephonyClass.getMethod("getSubscriberId");
            Method m2 = telephonyClass.getMethod("getSubscriberId", new Class[]{int.class});
            result1 = m1.invoke(telephony);
            result2 = m2.invoke(telephony, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            Log.e(TAG, " getSubscriberId : " + telephony.getSubscriberId() + "\n"
                    + " result1 : " + result1 + "\n"
                    + " result2 : " + result2 + "\n"
                    + " getSimSerialNumber : " + telephony.getSimSerialNumber() + "\n"
            );

        }
    }

}
